<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="../../plugins/layui/css/layui.css"/>
    <link rel="stylesheet" href="../../css/normalize.css"/>
    <link rel="stylesheet" href="../../css/fa.css"/>
    <link rel="stylesheet" href="../../css/icon.css"/>
    <link rel="stylesheet" href="../../css/ares_animate.min.css"/>
    <link rel="stylesheet" href="../../css/ares_hover_variant.min.css"/>
    <link rel="stylesheet" href="../../css/ares_reboot.css"/>
    <link rel="stylesheet" href="../../css/app.min.css"/>
    <link rel="stylesheet" href="../../css/iframe.css"/>

    <style>
        .layui-elem-quote {
            margin-bottom: 0;
        }

        .layui-card {
            border-radius: 5px;
            box-shadow: 0 2px 6px rgba(0, 0, 0, .1), 0 0 6px rgba(0, 0, 0, .1);
        }
    </style>
</head>
<body>
<!--基本信息-->
<h3>基本信息</h3>
<div class="ares-mb emr-info"></div>

<!--问题与目标-->
<blockquote class="layui-elem-quote">
    <span>问题与目标(选填)</span>
    <div id="problem-tool-btns" class="ares-float-right">
        <button class="create layui-btn layui-btn-xs">添加</button>
        <button class="update created layui-hide layui-btn layui-btn-normal layui-btn-xs">编辑</button>
        <button class="remove created layui-hide layui-btn layui-btn-danger layui-btn-xs">删除</button>
    </div>
</blockquote>
<div class="ares-p">
    <div id="problem-info" class="layui-card"></div>
</div>

<!--注意事项-->
<blockquote class="layui-elem-quote">
    <span>注意事项(选填)</span>
    <div id="notice-tool-btns" class="ares-float-right">
        <button class="create layui-btn layui-btn-xs">添加</button>
        <button class="update created layui-hide layui-btn layui-btn-normal layui-btn-xs">编辑</button>
        <button class="remove created layui-hide layui-btn layui-btn-danger layui-btn-xs">删除</button>
    </div>
</blockquote>
<div class="ares-p">
    <div id="notice-info" class="layui-card"></div>
</div>

<!--类别与项目|套餐-->
<div class="ares-row">
    <div class="ares-col-6 ares-mb-0">
        <blockquote class="layui-elem-quote">
            <span>类别与项目</span>
            <div id="type-tool-btns" class="ares-float-right">
                <button class="create layui-btn layui-btn-xs">添加</button>
                <button class="update created layui-hide layui-btn layui-btn-normal layui-btn-xs">编辑</button>
                <button class="remove created layui-hide layui-btn layui-btn-danger layui-btn-xs">删除</button>
            </div>
        </blockquote>
    </div>
    <div class="ares-col-6 ares-mb-0">
        <blockquote class="layui-elem-quote">
            <span>套餐</span>
            <div id="combo-tool-btns" class="ares-float-right">
                <button class="create layui-btn layui-btn-xs">添加</button>
                <button class="update created layui-hide layui-btn layui-btn-normal layui-btn-xs">编辑</button>
                <button class="remove created layui-hide layui-btn layui-btn-danger layui-btn-xs">删除</button>
            </div>
        </blockquote>
    </div>
</div>
<div class="ares-p">
    <div id="type-combo-info" class="layui-card"></div>
</div>

<!--人员安排-->
<blockquote class="layui-elem-quote">
    <span>人员安排</span>
    <div id="therapist-tool-btns" class="ares-float-right layui-hide">
        <button class="create layui-btn layui-btn-xs">添加</button>
        <button class="update created layui-hide layui-btn layui-btn-normal layui-btn-xs">编辑</button>
        <button class="remove created layui-hide layui-btn layui-btn-danger layui-btn-xs">删除</button>
    </div>
</blockquote>
<div class="ares-p">
    <div id="therapist-info" class="layui-card"></div>
</div>

<div class="ares-p ares-mt-lg">
    <div id="treatment-detail" class="layui-card layui-hide">
        <div class="layui-card-header">治疗项目详情</div>
        <div class="layui-card-body">
            <form class="layui-form" lay-filter="submit">
            </form>
        </div>
    </div>
</div>

</body>
<script type="text/javascript" src="../../plugins/layui/layui.js"></script>
<script type="text/javascript" src="../../aresExtends/config/layuiConfig.js"></script>
<script>
    layui.use(['form', 'utils', 'filters', 'comboServices', 'mandyServices', 'commonServices', 'qs'], function (form, utils, filters, comboServices, mandyServices, commonServices, qs) {
        var localHash = location.hash,
            queryStr = localHash.split('?')[1],
            routerParams = top.layui.routers.params,
            routerQuery = queryStr ? qs.parse(queryStr) : {},
            emrId = routerParams.emrId;
        console.log(localHash, queryStr, routerParams, routerQuery)

        init();

        /**
         * @author: ares
         * @date: 2021/3/10 10:39
         * @description: 监听问题与目标更新
         */
        utils.aresPageEventInit('updateTrainingProgramProblem', 'trainingProgramProblem', function (data) {
            utils.ARES.cache.trainingProgramProblem = data;
            renderSelectedProblemInfo();
        })

        /**
         * @author: ares
         * @date: 2021/3/10 11:14
         * @description: 点击添加问题与目标按钮弹出问题目标模态框
         */
        $('#problem-tool-btns .create').on('click', function () {
            location.reload()
            showProblemModal('添加问题与目标')
        })

        /**
         * @author: ares
         * @date: 2021/3/10 13:39
         * @description: 点击更新问题与目标按钮弹出问题目标模态框
         */
        $('#problem-tool-btns .update').on('click', function () {
            showProblemModal('更新问题与目标', true)
        })

        /**
         * @author: ares
         * @date: 2021/3/10 13:40
         * @description: 删除问题与目标
         */
        $('#problem-tool-btns .remove').on('click', function () {
            utils.ARES.cache.trainingProgramProblem = null;
            $('#problem-info').empty();
            toggleCreatedProblem(false);
        })

        /**
         * @author: ares
         * @date: 2021/3/9 9:22
         * @description: 显示问题与目标模态框
         * @param {string}title  显示模态框
         * @param {boolean}isEdit  是否是编辑操作
         */
        function showProblemModal(title, isEdit) {
            var queryStr = isEdit ? 1 : 0;
            utils.layerOpen({
                type: 2,
                title: title,
                area: ['800px', '500px'],
                content: 'pages/prcp/addTrainingComponents/problem.html?edit=' + queryStr,
                end: function (index, layero) {

                }
            });
        }

        /**
         * @author: ares
         * @date: 2021/3/10 14:34
         * @description: 切换是否已选择了问题与目标
         * @param{boolean} created  是否已经选择了数据
         */
        function toggleCreatedProblem(created) {
            if (created) {
                $('#problem-tool-btns .create').addClass('layui-hide');
                $('#problem-tool-btns .created').removeClass('layui-hide');
            } else {
                $('#problem-tool-btns .create').removeClass('layui-hide');
                $('#problem-tool-btns .created').addClass('layui-hide');
            }
        }

        /**
         * @author: ares
         * @date: 2021/3/10 14:02
         * @description: 渲染选择的问题与目标
         */
        function renderSelectedProblemInfo() {
            var cacheData = utils.ARES.cache.trainingProgramProblem;
            if (!cacheData || !$.isPlainObject(cacheData)) {
                toggleCreatedProblem(false);
                return;
            }
            var info = getSelectedProblem(),
                currentInfo = info.problem,
                shortInfo = info.short,
                langInfo = info.lang,
                $problemInfoWrap = $('#problem-info'),
                $currentFragment = $(document.createDocumentFragment()),
                $shortFragment = $(document.createDocumentFragment()),
                $langFragment = $(document.createDocumentFragment());

            if (!currentInfo.length && !shortInfo.length && !langInfo.length) {
                toggleCreatedProblem(false);
                return;
            }

            $problemInfoWrap.empty().append('<div class="layui-card-header">选择的问题与目标</div><div class="layui-card-body"></div>');

            if (currentInfo.length) {
                $currentFragment.append('<div class="problem-type"><span>当前主要问题: </span></div>');
                $.each(currentInfo, function (index, item) {
                    $currentFragment.find('.problem-type').append('<span class="ares-mr-lg">' + (++index) + '、' + item + '; </span>')
                })
            }
            if (shortInfo.length) {
                $shortFragment.append('<div class="problem-type"><span>短期目标: </span></div>');
                $.each(shortInfo, function (index, item) {
                    $shortFragment.find('.problem-type').append('<span class="ares-mr-lg">' + (++index) + '、' + item + '; </span>')
                })
            }
            if (langInfo.length) {
                $langFragment.append('<div class="problem-type"><span>长期目标: </span></div>');
                $.each(langInfo, function (index, item) {
                    $langFragment.find('.problem-type').append('<span class="ares-mr-lg">' + (++index) + '、' + item + '; </span>')
                })
            }

            $problemInfoWrap.find('.layui-card-body').empty().append($currentFragment, $shortFragment, $langFragment);

            toggleCreatedProblem(true);
        }

        /**
         * @author: ares
         * @date: 2021/3/11 12:53
         * @description: 获取选中的问题与目标
         */
        function getSelectedProblem() {
            var res = {
                    problem: [],
                    short: [],
                    lang: [],
                },
                cacheData = utils.ARES.cache.trainingProgramProblem;
            if (!cacheData || !$.isPlainObject(cacheData)) return res;

            for (key in cacheData) {
                if (cacheData[key].length == 0) continue;
                if (key.indexOf('current') == 0) {
                    res.problem.push(cacheData[key])
                } else if (key.indexOf('short') == 0) {
                    res.short.push(cacheData[key])
                } else if (key.indexOf('lang') == 0) {
                    res.lang.push(cacheData[key])
                }
            }
            return res;
        }

        /**
         * @author: ares
         * @date: 2021/3/10 10:43
         * @description: 监听注意事项更新
         */
        utils.aresPageEventInit('updateTrainingProgramNotice', 'trainingProgramNotice', function (data) {
            utils.ARES.cache.trainingProgramNotice = data;
            renderSelectedNoticeInfo();
        })

        /**
         * @author: ares
         * @date: 2021/3/10 11:14
         * @description: 点击添加注意事项按钮弹出注意事项模态框
         */
        $('#notice-tool-btns .create').on('click', function () {
            showNoticeModal('添加注意事项')
        })

        /**
         * @author: ares
         * @date: 2021/3/10 13:39
         * @description: 点击更新注意事项按钮弹出注意事项模态框
         */
        $('#notice-tool-btns .update').on('click', function () {
            showNoticeModal('更新注意事项', true)
        })

        /**
         * @author: ares
         * @date: 2021/3/10 13:40
         * @description: 删除注意事项
         */
        $('#notice-tool-btns .remove').on('click', function () {
            utils.ARES.cache.trainingProgramNotice = null;
            $('#notice-info').empty();
            toggleCreatedNotice(false);
        })

        /**
         * @author: ares
         * @date: 2021/3/9 9:22
         * @description: 显示注意事项模态框
         * @param {string}title  显示模态框
         * @param {boolean}isEdit  是否是编辑操作
         */
        function showNoticeModal(title, isEdit) {
            var queryStr = isEdit ? 1 : 0;
            utils.layerOpen({
                type: 2,
                title: title,
                area: ['800px', '500px'],
                content: 'pages/prcp/addTrainingComponents/notice.html?edit=' + queryStr,
                end: function (index, layero) {

                }
            });
        }

        /**
         * @author: ares
         * @date: 2021/3/10 14:34
         * @description: 切换是否已选择了注意事项
         * @param{boolean} created  是否已经选择了数据
         */
        function toggleCreatedNotice(created) {
            if (created) {
                $('#notice-tool-btns .create').addClass('layui-hide');
                $('#notice-tool-btns .created').removeClass('layui-hide');
            } else {
                $('#notice-tool-btns .create').removeClass('layui-hide');
                $('#notice-tool-btns .created').addClass('layui-hide');
            }
        }

        /**
         * @author: ares
         * @date: 2021/3/10 14:02
         * @description: 渲染选择的注意事项
         */
        function renderSelectedNoticeInfo() {
            var cacheData = utils.ARES.cache.trainingProgramNotice;
            if (!cacheData || !$.isPlainObject(cacheData)) {
                toggleCreatedProblem(false);
                return;
            }
            var noticeInfo = getSelectedNotice(),
                $noticeInfoWrap = $('#notice-info'),
                $noticeFragment = $(document.createDocumentFragment());

            if (!noticeInfo.length) {
                toggleCreatedProblem(false);
                return;
            }

            $noticeInfoWrap.empty().append('<div class="layui-card-header">选择的注意事项</div><div class="layui-card-body"></div>');

            if (noticeInfo.length) {
                $noticeFragment.append('<div class="problem-type"><span>注意事项: </span></div>');
                $.each(noticeInfo, function (index, item) {
                    $noticeFragment.find('.problem-type').append('<span class="ares-mr-lg">' + (++index) + '、' + item + '; </span>')
                })
            }

            $noticeInfoWrap.find('.layui-card-body').empty().append($noticeFragment);

            toggleCreatedNotice(true);
        }

        /**
         * @author: ares
         * @date: 2021/3/11 12:53
         * @description: 获取选中的注意事项
         */
        function getSelectedNotice() {
            var res = [],
                cacheData = utils.ARES.cache.trainingProgramNotice;
            if (!cacheData || !$.isPlainObject(cacheData)) return res;

            for (key in cacheData) {
                if (cacheData[key].length == 0) continue;
                if (key.indexOf('notice') == 0) {
                    res.push(cacheData[key])
                }
            }
            return res;
        }

        /**
         * @author: ares
         * @date: 2021/3/10 10:40
         * @description: 监听类别与项目更新
         */
        utils.aresPageEventInit('updateTrainingProgramType', 'trainingProgramType', function (data) {
            utils.ARES.cache.trainingProgramType = data;
            renderSelectedTypeInfo();
        })

        /**
         * @author: ares
         * @date: 2021/3/10 11:14
         * @description: 点击添加类别与项目按钮弹出类别与项目模态框
         */
        $('#type-tool-btns .create').on('click', function () {
            showTypeModal('添加类别与项目')
        })

        /**
         * @author: ares
         * @date: 2021/3/10 13:39
         * @description: 点击更新类别与项目按钮弹出类别与项目模态框
         */
        $('#type-tool-btns .update').on('click', function () {
            showTypeModal('更新类别与项目', true)
        })

        /**
         * @author: ares
         * @date: 2021/3/10 13:40
         * @description: 删除类别与项目
         */
        $('#type-tool-btns .remove').on('click', function () {
            utils.ARES.cache.trainingProgramType = null;
            $('#type-combo-info').empty();
            toggleCreatedType(false);
            showTypeOrComboTools();
            resetTherapistToolAndInfo(false);
        })

        /**
         * @author: ares
         * @date: 2021/3/9 9:22
         * @description: 显示类别与项目模态框
         * @param {string}title  显示模态框
         * @param {boolean}isEdit  是否是编辑操作
         */
        function showTypeModal(title, isEdit) {
            var queryStr = isEdit ? 1 : 0;
            utils.layerOpen({
                type: 2,
                title: title,
                area: ['800px', '500px'],
                content: 'pages/prcp/addTrainingComponents/type.html?edit=' + queryStr,
                end: function (index, layero) {

                }
            });
        }

        /**
         * @author: ares
         * @date: 2021/3/10 14:34
         * @description: 切换是否已选择了类别与项目
         * @param{boolean} created  是否已经选择了数据
         */
        function toggleCreatedType(created) {
            if (created) {
                $('#type-tool-btns .create').addClass('layui-hide');
                $('#type-tool-btns .created').removeClass('layui-hide');
            } else {
                $('#type-tool-btns .create').removeClass('layui-hide');
                $('#type-tool-btns .created').addClass('layui-hide');
            }
        }

        /**
         * @author: ares
         * @date: 2021/3/10 14:02
         * @description: 渲染选择的类别与项目
         */
        function renderSelectedTypeInfo() {
            var cacheData = utils.ARES.cache.trainingProgramType;
            if (!cacheData || !$.isPlainObject(cacheData)) {
                toggleCreatedType(false);
                resetTherapistToolAndInfo(false);
                return;
            }

            var trainingItemInfo = getSelectedTypeTrainingItem(),
                $typeComboInfoWrap = $('#type-combo-info'),
                $trainingItemFragment = $(document.createDocumentFragment());
            $trainingItemFragment.append('<div class="ares-mb">'
                + '<span class="ares-mr">处方类别: ' + cacheData.groupInfo.prcp_item_type.name + '</span>'
                + '</div>');

            $typeComboInfoWrap.empty().append('<div class="layui-card-header">选择的类别与项目</div><div class="layui-card-body"></div>');

            if (trainingItemInfo.length) {
                $trainingItemFragment.append('<div class="training-item"><span>治疗项目: </span></div>');
                $.each(trainingItemInfo, function (index, item) {
                    $trainingItemFragment.find('.training-item').append('<span class="ares-mr-lg">' + (++index) + '、' + item + '; </span>')
                })
            }

            $typeComboInfoWrap.find('.layui-card-body').empty().append($trainingItemFragment);

            showTypeOrComboTools('type');
            toggleCreatedType(true);
            resetTherapistToolAndInfo(true);
        }

        /**
         * @author: ares
         * @date: 2021/3/11 12:53
         * @description: 获取选中的类别与项目
         */
        function getSelectedTypeTrainingItem() {
            var res = [],
                cacheData = utils.ARES.cache.trainingProgramType;
            if (!cacheData || !$.isPlainObject(cacheData)) return res;
            for (key in cacheData) {
                if (cacheData[key].length == 0) continue;
                if (key.indexOf('treatmentItem') == 0) {
                    res.push(cacheData[key])
                }
            }
            return res;
        }

        /**
         * @author: ares
         * @date: 2021/3/10 10:41
         * @description: 监听套餐更新
         */
        utils.aresPageEventInit('updateTrainingProgramCombo', 'trainingProgramCombo', function (data) {
            console.log('combo', data)
            utils.ARES.cache.trainingProgramCombo = data;
            renderSelectedComboInfo();
        })

        /**
         * @author: ares
         * @date: 2021/3/10 11:14
         * @description: 点击添加套餐按钮弹出套餐模态框
         */
        $('#combo-tool-btns .create').on('click', function () {
            showComboModal('添加套餐')
        })

        /**
         * @author: ares
         * @date: 2021/3/10 13:39
         * @description: 点击更新套餐按钮弹出套餐模态框
         */
        $('#combo-tool-btns .update').on('click', function () {
            showComboModal('更新套餐', true)
        })

        /**
         * @author: ares
         * @date: 2021/3/10 13:40
         * @description: 删除套餐
         */
        $('#combo-tool-btns .remove').on('click', function () {
            utils.ARES.cache.trainingProgramCombo = null;
            $('#type-combo-info').empty();
            toggleCreatedCombo(false);
            showTypeOrComboTools();
            resetTherapistToolAndInfo(false);
        })

        /**
         * @author: ares
         * @date: 2021/3/9 9:22
         * @description: 显示套餐模态框
         * @param {string}title  显示模态框
         * @param {boolean}isEdit  是否是编辑操作
         */
        function showComboModal(title, isEdit) {
            var queryStr = isEdit ? 1 : 0;
            utils.layerOpen({
                type: 2,
                title: title,
                area: ['800px', '500px'],
                content: 'pages/prcp/addTrainingComponents/combo.html?edit=' + queryStr,
                end: function (index, layero) {

                }
            });
        }

        /**
         * @author: ares
         * @date: 2021/3/10 14:34
         * @description: 切换是否已选择了套餐
         * @param{boolean} created  是否已经选择了数据
         */
        function toggleCreatedCombo(created) {
            if (created) {
                $('#combo-tool-btns .create').addClass('layui-hide');
                $('#combo-tool-btns .created').removeClass('layui-hide');
            } else {
                $('#combo-tool-btns .create').removeClass('layui-hide');
                $('#combo-tool-btns .created').addClass('layui-hide');
            }
        }

        /**
         * @author: ares
         * @date: 2021/3/10 14:02
         * @description: 渲染选择的套餐
         */
        function renderSelectedComboInfo() {
            var cacheData = utils.ARES.cache.trainingProgramCombo;
            if (!cacheData || !$.isPlainObject(cacheData)) {
                toggleCreatedType(false);
                return;
            }

            var trainingItemInfo = cacheData.trainingItems,
                $typeComboInfoWrap = $('#type-combo-info'),
                $trainingItemFragment = $(document.createDocumentFragment());
            $trainingItemFragment.append('<div class="ares-mb">'
                + '<span class="ares-mr">套餐: ' + cacheData.comboInfo.name + '</span>'
                + '</div>');

            $typeComboInfoWrap.empty().append('<div class="layui-card-header">选择的套餐</div><div class="layui-card-body"></div>');

            if (trainingItemInfo.length) {
                $trainingItemFragment.append('<div class="training-item"><span>治疗项目: </span></div>');
                $.each(trainingItemInfo, function (index, item) {
                    $trainingItemFragment.find('.training-item').append('<span class="ares-mr-lg">' + (++index) + '、' + item.itemName + '; </span>')
                })
            }

            $typeComboInfoWrap.find('.layui-card-body').empty().append($trainingItemFragment);

            showTypeOrComboTools('combo');
            toggleCreatedCombo(true);
            resetTherapistToolAndInfo(true);
        }

        /**
         * @author: ares
         * @date: 2021/3/10 20:18
         * @description: 切换显示类别与项目|套餐的操作按钮
         * @param{string} type  显示类别与项目操作按钮|套餐按钮  type|combo
         */
        function showTypeOrComboTools(type) {
            if (type == 'type') {
                $('#type-tool-btns').removeClass('layui-hide');
                $('#combo-tool-btns').addClass('layui-hide');
                utils.ARES.cache.trainingProgramCombo = null;
            } else if (type == 'combo') {
                $('#type-tool-btns').addClass('layui-hide');
                $('#combo-tool-btns').removeClass('layui-hide');
                utils.ARES.cache.trainingProgramType = null;
            } else {
                $('#type-tool-btns').removeClass('layui-hide');
                $('#combo-tool-btns').removeClass('layui-hide');
                utils.ARES.cache.trainingProgramType = null;
                utils.ARES.cache.trainingProgramCombo = null;
            }
            $('#type-tool-btns .create').removeClass('layui-hide');
            $('#type-tool-btns .created').addClass('layui-hide');
            $('#combo-tool-btns .create').removeClass('layui-hide');
            $('#combo-tool-btns .created').addClass('layui-hide');
        }

        /**
         * @author: ares
         * @date: 2021/3/10 10:41
         * @description: 监听治疗师更新
         */
        utils.aresPageEventInit('updateTrainingProgramTherapist', 'trainingProgramTherapist', function (data) {
            console.log('therapist', data)
            utils.ARES.cache.trainingProgramTherapist = data;
            renderSelectedTherapistInfo();
        })

        /**
         * @author: ares
         * @date: 2021/3/10 11:14
         * @description: 点击添加治疗人员按钮弹出治疗人员模态框
         */
        $('#therapist-tool-btns .create').on('click', function () {
            var cacheTypeData = utils.ARES.cache.trainingProgramType,
                cacheComboData = utils.ARES.cache.trainingProgramCombo;
            if ((!cacheTypeData || !$.isPlainObject(cacheTypeData)) && (!cacheComboData || !$.isPlainObject(cacheComboData))) {
                utils.msgError('请先选择类别与项目或选择套餐');
                return;
            }
            showTherapistModal('添加治疗人员')
        })

        /**
         * @author: ares
         * @date: 2021/3/10 13:39
         * @description: 点击更新治疗人员按钮弹出治疗人员模态框
         */
        $('#therapist-tool-btns .update').on('click', function () {
            showTherapistModal('更新治疗人员', true);
        })

        /**
         * @author: ares
         * @date: 2021/3/10 13:40
         * @description: 删除治疗人员
         */
        $('#therapist-tool-btns .remove').on('click', function () {
            utils.ARES.cache.trainingProgramTherapist = null;
            $('#therapist-info').empty();
            toggleCreatedTherapist(false);
            resetTreatmentDetail();
        })

        /**
         * @author: ares
         * @date: 2021/3/9 9:22
         * @description: 显示治疗人员模态框
         * @param {string}title  显示模态框
         * @param {boolean}isEdit  是否是编辑操作
         */
        function showTherapistModal(title, isEdit) {
            var queryStr = isEdit ? 1 : 0;
            utils.layerOpen({
                type: 2,
                title: title,
                area: ['800px', '500px'],
                content: 'pages/prcp/addTrainingComponents/therapist.html?emrId=' + emrId + '&edit=' + queryStr,
                end: function (index, layero) {

                }
            });
        }

        /**
         * @author: ares
         * @date: 2021/3/10 14:34
         * @description: 切换是否已选择了治疗人员
         * @param{boolean} created  是否已经选择了数据
         */
        function toggleCreatedTherapist(created) {
            if (created) {
                $('#therapist-tool-btns .create').addClass('layui-hide');
                $('#therapist-tool-btns .created').removeClass('layui-hide');
            } else {
                $('#therapist-tool-btns .create').removeClass('layui-hide');
                $('#therapist-tool-btns .created').addClass('layui-hide');
            }
        }

        /**
         * @author: ares
         * @date: 2021/3/10 14:02
         * @description: 渲染选择的治疗人员
         */
        function renderSelectedTherapistInfo() {
            var cacheData = utils.ARES.cache.trainingProgramTherapist;
            if (!cacheData || !$.isPlainObject(cacheData)) {
                toggleCreatedTherapist(false);
                resetTreatmentDetail();
                return;
            }

            var timeBlockInfo = getSelectedTimeBlock(),
                $therapistInfoWrap = $('#therapist-info'),
                $therapistFragment = $(document.createDocumentFragment());
            $therapistFragment.append('<div class="ares-mb">'
                + '<span class="ares-mr">治疗师: ' + cacheData.therapistInfo.name + '</span>'
                + '<span class="ares-mr">治疗组: ' + cacheData.groupInfo.name + '</span>'
                + '<span class="ares-mr">治疗师负责患者数: ' + cacheData.therapistInfo.total_patient_number + '</span>'
                + '<span class="ares-mr">治疗频次: 1日' + cacheData.frequency + '次</span>'
                + '<span class="ares-mr">治疗次数: ' + cacheData.times + '次</span>'
                + '</div>');

            $therapistInfoWrap.empty().append('<div class="layui-card-header">选择的治疗人员</div><div class="layui-card-body"></div>');
            $therapistFragment.append('<div class="training-date"><span>治疗日期: ' + cacheData.treatmentDay + '</span></div>');
            if (timeBlockInfo.length) {
                $therapistFragment.append('<div class="training-time"><span>治疗时间: </span></div>');
                $.each(timeBlockInfo, function (index, item) {
                    $therapistFragment.find('.training-time').append('<span class="ares-mr-lg">' + item + '</span>')
                })
            }

            $therapistInfoWrap.find('.layui-card-body').empty().append($therapistFragment);

            toggleCreatedTherapist(true);
            showTreatmentDetail();
        }

        /**
         * @author: ares
         * @date: 2021/3/11 13:49
         * @description: 获取选择的时间块
         */
        function getSelectedTimeBlock() {
            var res = [],
                cacheData = utils.ARES.cache.trainingProgramTherapist;
            if (!cacheData || !$.isPlainObject(cacheData)) return res;
            for (key in cacheData) {
                if (cacheData[key].length == 0) continue;
                if (key.indexOf('timeBlock') == 0) {
                    res.push(cacheData[key])
                }
            }
            return res;
        }

        /**
         * @author: ares
         * @date: 2021/3/11 10:12
         * @description: 重置治疗人员按钮和信息
         * @param{boolean}showTool 显示按钮
         */
        function resetTherapistToolAndInfo(showTool) {
            if (showTool) {
                $('#therapist-tool-btns').removeClass('layui-hide');
            } else {
                $('#therapist-tool-btns').addClass('layui-hide');
            }
            $('#therapist-tool-btns .create').removeClass('layui-hide');
            $('#therapist-tool-btns .created').addClass('layui-hide');
            utils.ARES.cache.trainingProgramTherapist = null;
            $('#therapist-info').empty();
            resetTreatmentDetail();
        }

        /**
         * @author: ares
         * @date: 2021/3/11 11:26
         * @description: 重置治疗详情为默认状态
         */
        function resetTreatmentDetail() {
            var $treatmentDetailWrap = $('#treatment-detail'),
                $treatmentDetailForm = $treatmentDetailWrap.find('form');
            $treatmentDetailForm.empty();
            $treatmentDetailWrap.addClass('layui-hide');
        }

        /**
         * @author: ares
         * @date: 2021/3/11 10:37
         * @description: 显示治疗详情,选择治疗部位,治疗方法,治疗剂量
         */
        function showTreatmentDetail() {
            var cacheTypeData = utils.ARES.cache.trainingProgramType,
                cacheComboData = utils.ARES.cache.trainingProgramCombo,
                cacheTherapistData = utils.ARES.cache.trainingProgramTherapist,
                isCombo = cacheComboData && $.isPlainObject(cacheComboData) ? true : false,
                cacheData = isCombo ? cacheComboData : cacheTypeData,
                trainingItem = [],
                $treatmentDetailWrap = $('#treatment-detail'),
                $treatmentDetailForm = $treatmentDetailWrap.find('form'),
                $treatmentDetailFragment = $(document.createDocumentFragment());
            console.log(cacheTypeData,cacheComboData,cacheTherapistData)
            if (!cacheData || !$.isPlainObject(cacheData)) return;

            if (isCombo) {
                $.each(cacheData.trainingItems, function (i, item) {
                    trainingItem.push(item.itemName);
                })
            } else {
                trainingItem = getSelectedTypeTrainingItem();
            }

            $.each(trainingItem, function (index, item) {
                $treatmentDetailFragment.append('<div class="layui-form-item">'
                    + '<div><b>' + item + '</b></div>'
                    + '<div class="layui-inline">'
                    + '<label class="layui-form-label">治疗部位</label>'
                    + '<div class="layui-input-inline">'
                    + '<select name="part-' + index + '" lay-verify="required">'
                    + '<option value="患侧肢体">患侧肢体</option>'
                    + '<option value="躯体">躯体</option>'
                    + '<option value="全身">全身</option>'
                    + '<option value="左侧上肢">左侧上肢</option>'
                    + '<option value="右侧上肢">右侧上肢</option>'
                    + '<option value="双侧上肢">双侧上肢</option>'
                    + '<option value="左侧下肢">左侧下肢</option>'
                    + '<option value="右侧下肢">右侧下肢</option>'
                    + '<option value="双侧下肢">双侧下肢</option>'
                    + '<option value="其他">其他</option>'
                    + '</select>'
                    + '</div>'
                    + '<div class="layui-inline">'
                    + '<label class="layui-form-label">治疗方法</label>'
                    + '<div class="layui-input-inline">'
                    + '<input  name="method-' + index + '" class="layui-input">'
                    + '</div>'
                    + '</div>'
                );
                if (cacheTherapistData.groupInfo.prcp_item_type.name == '理疗治疗单') {
                    $treatmentDetailFragment.find('layui-form-item').append('<div class="layui-inline">'
                        + '<label class="layui-form-label">剂量</label>'
                        + '<div class="layui-input-inline">'
                        + '<select name="dosage-' + index + '" lay-verify="required">'
                        + '<option value="以病人有舒适的温热感为准">以病人有舒适的温热感为准</option>'
                        + '<option value="运动阈值以下">运动阈值以下</option>'
                        + '<option value="运动阈值">运动阈值</option>'
                        + '<option value="运动阈值以上">运动阈值以上</option>'
                        + '<option value="无热量">无热量</option>'
                        + '<option value="微热量">微热量</option>'
                        + '<option value="温热量">温热量</option>'
                        + '<option value="热量">热量</option>'
                        + '<option value="固定法，小剂量0.1~0.2">固定法，小剂量0.1~0.2</option>'
                        + '<option value="固定法，中剂量0.3~0.4">固定法，中剂量0.3~0.4</option>'
                        + '<option value="固定法，大剂量0.5~0.8">固定法，大剂量0.5~0.8</option>'
                        + '<option value="移动法，小剂量0.5~0.8">移动法，小剂量0.5~0.8</option>'
                        + '<option value="移动法，中剂量0.9~1.2">移动法，中剂量0.9~1.2</option>'
                        + '<option value="移动法，大剂量1.3~2.0">移动法，大剂量1.3~2.0</option>'
                        + '<option value="耐受">耐受</option>'
                        + '<option value="颈椎6Kg开始，每日可增加1Kg，一般不超过20Kg。腰椎从体重的40％开始">颈椎6Kg开始，每日可增加1Kg，一般不超过20Kg。腰椎从体重的40％开始</option>'
                        + '<option value="45℃">45℃</option>'
                        + '<option value="43℃">43℃</option>'
                        + '<option value="中等剂量">中等剂量</option>'
                        + '<option value="50mmHg">50mmHg</option>'
                        + '</select>'
                        + '</div>'
                    );
                }
            })
            $treatmentDetailFragment.append('<div class="layui-form-item">'
                + '<div class="layui-input-block">'
                + '<button class="layui-btn" lay-submit lay-filter="submit">新建训练处方</button>'
                + '</div>'
                + '</div>');
            $treatmentDetailForm.empty().append($treatmentDetailFragment);
            $treatmentDetailWrap.removeClass('layui-hide');
            form.render();
        }

        /**
         * @author: ares
         * @date: 2021/3/9 9:52
         * @description: 提交表单,新建训练处方
         */
        form.on('submit(submit)', function (data) {
            var cacheTypeData = utils.ARES.cache.trainingProgramType,
                cacheComboData = utils.ARES.cache.trainingProgramCombo,
                cacheTherapistData = utils.ARES.cache.trainingProgramTherapist,
                fields = data.field,
                $this = $(data.elem),
                isPackage = cacheComboData ? true : false,
                problemInfo = getSelectedProblem(),
                result = {
                    type: 2,
                    medical_record: {id: emrId},
                    train_dto: {
                        pro_list: {
                            problem_content: problemInfo.problem.join(','),
                            short_objective_content: problemInfo.short.join(','),
                            lang_objective_content: problemInfo.lang.join(','),
                        },
                        training_list: [{
                            attention: getSelectedNotice().join(','),
                        }]
                    }
                },
                tempObj = {},
                treatmentTime = {
                    timeBucket: [],
                    time_bucket: {},
                    txt: ''
                },
                part = [],
                trainingItem = [];
            tempObj.group_id = cacheTherapistData.groupInfo.id;
            tempObj.prcp_item_type_id = cacheTherapistData.groupInfo.prcp_item_type.id;
            tempObj.prcp_item_type_name = cacheTherapistData.groupInfo.prcp_item_type.name;
            tempObj.treatment_times = cacheTherapistData.times;
            tempObj.treatment_frequency = {txt: filters.treatmentFrequencyFormat(cacheTherapistData.frequency), value: cacheTherapistData.frequency};
            tempObj.therapist_id = cacheTherapistData.therapistInfo.id;
            tempObj.therapist_name = cacheTherapistData.therapistInfo.name;
            for (key in cacheTherapistData) {
                if (cacheTherapistData[key].length == 0) continue;
                if (key.indexOf('timeBlock') == 0) {
                    treatmentTime.txt += (',' + cacheTherapistData.treatmentDay + ' ' + cacheTherapistData[key]);
                    treatmentTime.timeBucket.push(cacheTherapistData.allTimeBlockList[key.split('-')[1]]);
                    treatmentTime.time_bucket[key.split('-')[1]] = cacheTherapistData.allTimeBlockList[key.split('-')[1]];
                }
            }
            treatmentTime.txt = treatmentTime.txt.substr(1)
            if (isPackage) {
                $.each(cacheComboData.trainingItems, function (index, item) {
                    trainingItem.push(item.itemName);
                })
            } else {
                trainingItem = getSelectedTypeTrainingItem();
            }
            tempObj.treatment_time = treatmentTime;
            $.each(trainingItem, function (index, item) {
                part.push({
                    title: index + '、' + item,
                    item: item,
                    part: fields['part-' + index],
                    method: fields['method-' + index],
                    dosage: fields['dosage-' + index],
                })
            })
            tempObj.part = part;
            $.extend(result.train_dto.training_list[0], tempObj);
            result.description = JSON.stringify(result.train_dto);
            console.log(result);
            $this.prop('disabled', true).addClass('layui-btn-disabled');
            mandyServices.mandy_post_add_package_prescription(isPackage, result).then(function (res) {
                utils.confirm('是否继续添加训练处方?', {icon: 3, title: '添加成功'}, function (index) {
                    location.reload();
                    utils.layer.close(index);
                }, function (index) {
                    utils.layer.close(index);
                    utils.closeCurrentSysTab();
                });
                $this.prop('disabled', false).removeClass('layui-btn-disabled');
            }).catch(function (err) {
                $this.prop('disabled', false).removeClass('layui-btn-disabled')
            })
            return false;
        });

        /**
         * @author: ares
         * @date: 2021/3/10 10:49
         * @description: 获取患者基本信息
         */
        function getPatientInfo() {
            commonServices.getEmrInfoService(emrId, null, null, $('.emr-info'));
        }

        /**
         * @author: ares
         * @date: 2021/3/10 10:52
         * @description: 初始化
         */
        function init() {
            getPatientInfo();
            utils.ARES.cache.trainingProgramProblem = null;
            utils.ARES.cache.trainingProgramNotice = null;
            utils.ARES.cache.trainingProgramType = null;
            utils.ARES.cache.trainingProgramCombo = null;
            utils.ARES.cache.trainingProgramTherapist = null;
        }
    })
</script>


</html>